﻿namespace Jsl.FxCop
{
    using System.Linq;
    using Microsoft.FxCop.Sdk;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class TestCciHelper
    {
        [TestMethod]
        public void Can_get_branch_condition()
        {
            var compileHelper = new CompileHelper()
                .AddMethod(@"
public int Method(bool parameter)
{
    if (parameter)
    {
        return 5;
    }

    return 4;
}");
            new CodeTestHelper(compileHelper)
                .Test((debugMode, method) =>
                {
                    var flow = new StatementFlow(method);
                    var branch = flow.Statements
                        .OfType<Branch>()
                        .Where(b => b.Condition != null)
                        .Single();
                    var condition = branch.GetBranchCondition(flow);

                    if (debugMode)
                    {
                        var binaryExpression = (BinaryExpression)condition;
                        AssertEx.That(() => binaryExpression.NodeType == NodeType.Ceq);
                        AssertEx.That(() => binaryExpression.Operand1 is Parameter);
                    }
                    else
                    {
                        var unaryExpression = (UnaryExpression)condition;
                        AssertEx.That(() => unaryExpression.NodeType == NodeType.LogicalNot);
                        AssertEx.That(() => unaryExpression.Operand is Parameter);
                    }
                });
        }
    }
}